<?php

namespace App;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;

class Couplet extends Model
{
    protected $table = 'couplet';
    //插入用户获取的3个对联
    public static function insertUserCouplet($user,$couplet_id){
        DB::table('user_couplet')->insert(
            ['u_id'=>$user,'couplet_id'=>$couplet_id,'couplet_status'=>1,'add_time'=>time(),'is_use'=>0]
        );
    }
    public static function insertUid($user_id,$phone){
        $user = DB::table('myuser')->where([
            ['u_id','=',$user_id]
        ])->first();
        if(empty($user)){ 
            DB::table('myuser')->insert(
                ['u_id'=>$user_id,'phone'=>$phone,'unionid'=>'']
            );
        }
    }
    //获取所有对联
    public static function getCouplet(){
        $couplet = DB::table('couplet')->get();
        return $couplet;
    }
    //查看用户参与活动了没
    public static function getUserActivity($user){
        $count= DB::table('activity')->where([
            ['u_id','=',$user],
            ['status','=',1]
        ])->count();
        if($count){
            return true;
        }else{
            return false;
        }
    }
    //增加用户已参与活动
    public static function insertUserActivity($user){
        DB::table('activity')->insert(
            ['u_id'=>$user,'name'=>'获得3个对联','status'=>'1','share_status'=>'0']
        );
    }
    //获取用户的对联
    public static function getUserCouplet($user,$couplet_id=null){
        if($couplet_id){
            $where = array(
                ['user_couplet.u_id','=',$user],
                ['user_couplet.couplet_status','=',1],
                ['user_couplet.couplet_id','=',$couplet_id],
                ['user_couplet.is_use','=',0]
            );
            $user_couplet = DB::table('user_couplet')
            ->join('couplet','couplet.id','=','user_couplet.couplet_id')
            ->where($where)->select('couplet.couplet_name','couplet.id')->first();
        }else{
            $where = array(
                ['user_couplet.u_id','=',$user],
                ['user_couplet.couplet_status','=',1]
            );
            $user_couplet = DB::table('user_couplet')
            ->join('couplet','couplet.id','=','user_couplet.couplet_id')
            ->where($where)->select('couplet.couplet_name','couplet.id','user_couplet.couplet_status','user_couplet.is_use')->get();
        }
        return $user_couplet;
    }
    public static function getUserCoupletUsed($user){
        return DB::table('user_couplet')->where([
            ['u_id','=',$user],
            ['is_use','=',1]
        ])->get();
    }
    public static function getUserCoupletByid($user,$couplet1,$couplet2){
        $where1 = array(
                ['user_couplet.u_id','=',$user],
                ['user_couplet.couplet_status','=',1],
                ['user_couplet.couplet_id','=',$couplet1]
            );
        $where2 = array(
            ['user_couplet.u_id','=',$user],
            ['user_couplet.couplet_status','=',1],
            ['user_couplet.couplet_id','=',$couplet2]
        );
        $user_couplet = DB::table('user_couplet')
        ->join('couplet','couplet.id','=','user_couplet.couplet_id')
        ->where($where1)->orWhere($where2)->select('couplet.couplet_name','couplet.id','user_couplet.couplet_status','user_couplet.is_use')->get();
        return $user_couplet;
    }
    public static function updataUserCouplet($give_user,$couplet_id){
        $update = DB::table('user_couplet')->where([
            ['u_id','=',$give_user],
            ['couplet_status','=',1],
            ['couplet_id','=',$couplet_id]
        ])->update(
            ['couplet_status'=>0],
            ['add_time','=',time()]
        );
        if($update){
            return true;
        }else{
            return false;
        }
    }
    public static function addShareCouplet($give_user,$couplet_id){
        return DB::table('share_couplet')->insertGetId(
            ['give_id'=>$give_user,'couplet_id'=>$couplet_id,'add_time'=>time(),'status'=>0]
        );
    }
    public static function getUserShare($user,$couplet_id){
        return DB::table('share_couplet')->where([
          ['give_id','=',$user],
          ['couplet_id','=',$couplet_id],
          ['status','=',0]
        ])->first();
    }
    public static function updateUserShare($user,$couplet_id){
        DB::table('share_couplet')->where([
            ['give_id','=',$user],
            ['couplet_id','=',$couplet_id],
            ['status','=',0]
        ])->update(
            ['status'=>1]
        );
    }
    public static function getPrize($type,$prize_id=null){
        if($prize_id){
            $where = array(
                ['type','=',$type],
                ['id','=',$prize_id]
            );
        }else{
            $where = array(
                ['type','=',$type]
            );
        }
        $prize = DB::table('prize')->where($where)->get();
        return $prize;
    }
    public static function getQuantity($prize_id){
        return DB::table('prize')->where([
            ['id','=',$prize_id]
        ])->select('quantity')->first();
    }
    public static function getUserPrize($user){
        return DB::table('user_prize')->leftjoin('prize','user_prize.prize_id','=','prize.id')->where([
            ['u_id','=',$user]
        ])->orderBy('add_time','desc')->first();
    }
    public static function getPrizeBytype($type){
        return DB::table('prize')->where('type',$type)->get();
    }
    public static function addPrize($type,$prize_name,$prize_probability,$amount,$prize_type,$quantity){
        $res = DB::table('prize')->insert(
            ['type'=>$type,'prize_name'=>$prize_name,'prize_probability'=>$prize_probability,'time'=>time(),'amount'=>$amount,'prize_type'=>$prize_type,'quantity'=>$quantity]
        );
        return $res;
    }
    public static function updatePrize($type,$prize_id,$prize_name,$prize_probability,$amount,$prize_type,$quantity){
        $res = DB::table('prize')->where([
            ['id','=',$prize_id]
        ])->update(
            ['prize_type'=>$prize_type,'prize_probability'=>$prize_probability,'prize_name'=>$prize_name,'amount'=>$amount,'quantity'=>$quantity]
        );
        print_r($res);
        exit;
        return $res;
    }
    public static function insertUserPrize($u_id,$prize_id){
        DB::table('user_prize')->insert(
            ['u_id'=>$u_id,'prize_id'=>$prize_id,'add_time'=>time(),'status'=>0]
        );
    }
    public static function getMyPrize($user){
        return DB::table('user_prize')->leftjoin('prize','prize.id','=','user_prize.prize_id')->where([
            ['u_id','=',$user]
        ])->get();
    }
    public static function getIsUseCouplet($user,$couplet_id){
        return DB::table('user_couplet')->where([
            ['u_id','=',$user],
            ['couplet_id','=',$couplet_id],
            ['is_use','=',1]
        ])->first();
    }
    public static function updateAllPrize($user){
        DB::table('activity')->where([
            ['u_id','=',$user]
        ])->update(
            ['finish'=>1],
            ['add_time'=>time()]
        );
    }
    public static function finish($user){
        return DB::table('activity')->where([
            ['u_id','=',$user]
        ])->select('finish')->first();
    }
    public static function collect($user){
        return DB::table('user_couplet')->where([
            ['u_id','=',$user],
            ['couplet_status','=',1]
        ])->distinct()->select('couplet_id')->get();
    }
    public static function getFristPrize($user){
        return DB::table('user_prize')->where([
            ['u_id','=',$user]
        ])->first();
    }
    public static function updateUseCouplet($user,$couplet_id1,$couplet_id2){
        DB::table('user_couplet')->where([
            ['u_id','=',$user],
        ])->whereIn('couplet_id',[$couplet_id1,$couplet_id2])->update(
            ['is_use'=>1],
            ['add_time'=>time()]
        );
    }
    public static function decrementPrize($prize){
        DB::table('prize')->where([
            ['id','=',$prize]
        ])->decrement('quantity');
    }
    public static function getPeople($help_id){
        $helps= DB::table('user_help')->where([
            ['user_help.id','=',$help_id],
        ])->leftjoin('other_help','user_help.id','=','other_help.help_id')
        ->leftjoin('couplet','user_help.couplet_id','=','couplet.id')
        ->leftjoin('wx','other_help.other_id','=','wx.unionid')
        ->select('user_help.id','user_help.u_id','user_help.couplet_id','user_help.status','couplet.couplet_name','other_help.other_id','other_help.num','other_help.add_time','wx.img','wx.name')->get();
        return $helps;
    }
    public static function checkUserHelp($user,$couplet_id){
        return DB::table('user_help')->where([
            ['u_id','=',$user],
            ['couplet_id','=',$couplet_id],
            ['status','=',0]
        ])->count();
    }
    public static function insert_help($user,$couplet_id){
        $helps = DB::table('user_help')->where([
            ['u_id','=',$user],
            ['couplet_id','=',$couplet_id],
            ['add_time','=',date('Y-m-d')]
        ])->get();
        $length = count($helps);
        if($length>=2){
            return false;
        }else{
            $res = DB::table('user_help')->insertGetId(
                ['u_id'=>$user,'couplet_id'=>$couplet_id,'status'=>0,'add_time'=>date('Y-m-d')]
            );
            return $res;
        }
    }
    
    public static function other_help($user,$openid,$couplet_id,$help_id,$num){
        DB::table('other_help')->insert(
            ['other_id'=>$openid,'help_id'=>$help_id,'add_time'=>time(),'num'=>$num]
        );
        $people = DB::table('other_help')->where([
            ['help_id','=',$help_id]
        ])->sum('num');
        if($people>=60){
            DB::table('user_help')->where([
                ['id','=',$help_id],
                ['u_id','=',$user],
                ['couplet_id','=',$couplet_id],
                ['status','=',0]
            ])->update(
                ['status'=>1]
            );
            DB::table('user_couplet')->insert(['u_id'=>$user,'couplet_id'=>$couplet_id,'couplet_status'=>1,'add_time'=>time(),'is_use'=>0]);
        }
        return true;
    }

    public static function get_help_finish($user,$couplet_id,$help_id){
        return DB::table('user_help')->where([
            ['id','=',$help_id],
            ['u_id','=',$user],
            ['couplet_id','=',$couplet_id],
            ['status','=',0]
        ])->first();
    }

    public static function getJoin($openid,$help_id,$status){
        if($status === 0){
            return DB::table('other_help')->leftJoin(
                'user_help','user_help.id','=','other_help.help_id'
            )->where([
                ['other_id','=',$openid],
                ['help_id','=',$help_id],
                ['status','=',0]
            ])->count();
        }else{
            return DB::table('other_help')->leftJoin(
                'user_help','user_help.id','=','other_help.help_id'
            )->where([
                ['other_id','=',$openid],
                ['help_id','=',$help_id]
            ])->count();
        }
    }
    public static function insertOpenId($openid,$img,$nickname){
        $men=DB::table('wx')->where([
            ['unionid','=',$openid]
        ])->first();
        if(empty($men)){ 
            DB::table('wx')->insert(
                ['img'=>$img,'unionid'=>$openid,'name'=>$nickname]
            );
        }
    }
    public static function getMoney($u_id){
       return  $amount = DB::table('user_prize')->leftjoin(
            'prize','user_prize.prize_id','=','prize.id'
        )->where([
            ['prize.prize_type','=','0'],
            ['user_prize.status','=','0']
        ])->sum('prize.amount');
    }
    public static function updateMoney($u_id){
        $res = DB::table('user_prize')->leftjoin(
            'prize','user_prize.prize_id','=','prize.id'
        )->where([
            ['u_id','=',$u_id],
            ['prize_type','=','0'],
            ['status','=','0']
        ])->update(
            ['status'=>'1']
        );
        return $res;
    }
    public static function countOther($help_id){
        return $helps= DB::table('other_help')->where([
            ['help_id','=',$help_id]
        ])->count();
    }
    public static function getShareInfo($share_id){
        return $share_id = DB::table('share_couplet')->where([
            ['id','=',$share_id]
        ])->first();
    }
}
